<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">品牌</label>
                            <div class="layui-input-block">
                                <select name="brandType" id="brandType">
                                    <option value=>全部</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">微信号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="wechatNo" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">跟进状态</label>
                            <div class="layui-input-block">
                                <select name="status" id="selectStatus">
                                    <option value=>全部</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="accessTimeStart" id="accessTimeStart" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="accessTimeEnd" id="accessTimeEnd" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="mainTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="batchdelete">批量回收</a>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">回收</a>
        </script>

        <script type="text/html" id="empTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="edit" lay-even="edit">操作</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table,
            roleType = layui.data("user").roleType,
            basecols = [[
                {type: 'checkbox'},
                {field: 'followUpTimes', title: '跟进次数'},
                {field: 'phone', title: '手机号'},
                {field: 'brandTypeName', title: '品牌'},
                // {field: 'highSeasId', title: '公海id'},
                {field: 'statusName', title: '私海跟进状态'},
                {field: 'nickname', title: '昵称'},
                {field: 'remark', title: '备注'},
                {field: 'wechatNo', title: '微信号'},
                {field: 'accessTime', title: '接入时间'}
            ]],
            currPage = 1;

        //日期
        laydate.render({
            elem: '#accessTimeStart',
            type: 'datetime'
        });
        laydate.render({
            elem: '#accessTimeEnd',
            type: 'datetime'
        });

        if (roleType === "SUPERVISOR") {
            basecols[0].push({title: '操作', toolbar: '#currentTableBar', align: "center"});
        } else if (roleType === "EMPLOYEE") {
            basecols[0].push({title: '操作', toolbar: '#empTableBar', align: "center"});
        }

        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: '/zxjy/privateseas/list/' + layui.data("user").roleType.toLowerCase(),
            toolbar: roleType === "SUPERVISOR" ? '#mainTableBar' : '',
            contentType: 'application/json',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cellMinWidth: 100,
            cols: basecols,
            response: {
                statusCode: 200,
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.success, //解析提示文本
                    "count": res.result.total, //解析数据长度
                    "data": res.result.list //解析数据列表
                };
            },
            done: function (res, curr, count) {
                currPage = curr;
            },
            request: {
                pageName: 'pageNum' // 页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });


        $.ajax({
            type: "get",
            url: "/zxjy/enum/privateseasstatus",
            // url: '../api/status.json',
            headers: {
                'Authorization': layui.data("Authorization")['Authorization'],
            },
            success: function (result) {
                if (result.state === 200) {
                    for (obj of result.result) {
                        $("#selectStatus").append("<option value=\"" + obj.key + "\">" + obj.value + "</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }
            }
        });

        $.ajax({
            type: "get",
            url: "/zxjy/enum/brandtype",
            // url: '../api/status.json',
            headers: {
                'Authorization': layui.data("Authorization")['Authorization'],
            },
            success: function (result) {
                if (result.state === 200) {
                    for (obj of result.result) {
                        $("#brandType").append("<option value=\"" + obj.key + "\">" + obj.value + "</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            currPage = 1;
            table.reload('currentTableId', {
                url: '/zxjy/privateseas/list/' + layui.data("user").roleType.toLowerCase(),
                page: {
                    curr: currPage //重新从第 1 页开始
                },
                where: {
                    wechatNo: data.field.wechatNo,
                    phone: data.field.phone,
                    nickname: data.field.nickname,
                    highSeasStatus: data.field.highSeasStatus,
                    accessTimeEnd: data.field.accessTimeEnd,
                    accessTimeStart: data.field.accessTimeStart,
                    brandType: '' === data.field.brandType ? null : data.field.brandType,
                    status: '' === data.field.status ? null : data.field.status,
                }
            });
            return false;
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            switch (obj.event) {
                case 'batchdelete':
                    let checkStatus = table.checkStatus('currentTableId');
                    let selectedData = checkStatus.data;
                    if (selectedData.length === 0) {
                        layer.alert("请选择一条私海数据");
                        return;
                    }
                    layer.confirm('是否确认批量回收?', function (index) {
                        let params = {};
                        params.privateSeasIds = selectedData.map(data => data.id);
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/zxjy/privateseas/batchrecycle/supervisor",
                            data: JSON.stringify(params),
                            headers: {
                                'Authorization': layui.data("Authorization")['Authorization'],
                            },
                            success: function (result) {
                                if (result.state === 200) {
                                    table.reload('currentTableId');
                                    layer.close(index);
                                    layer.alert("批量回收成功");
                                }

                                table.reload('currentTableId');
                            }
                        });
                    });
                    break;
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'delete':
                    var param = {}
                    param["privateSeasId"] = data["id"];
                    layer.confirm('是否确认回收?', function (index) {
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "/zxjy/privateseas/recycle/supervisor",
                            data: JSON.stringify(param),
                            headers: {
                                'Authorization': layui.data("Authorization")['Authorization'],
                            },
                            success: function (result) {
                                if (result.state === 200) {
                                    obj.del();
                                    layer.close(index);
                                    layer.alert("回收成功")
                                }
                            }
                        });

                    });
                    break;
                case 'edit':
                    layer.open({
                        title: '修改',
                        type: 2,
                        shadeClose: true,
                        offset: '150px',
                        area: ['550px', '500px'],
                        content: '../page/table/editprivatedata.html',
                        success: function (layero, index) {
                            //此id为页面层input标签id
                            var jquerySendHelloButton = $("#sid", layero.find("iframe")[0].contentWindow.document);
                            //要传的值
                            jquerySendHelloButton.attr("value", data.id);
                            $("#wechatNo", layero.find("iframe")[0].contentWindow.document).attr("value", data.wechatNo);
                            $("#remark", layero.find("iframe")[0].contentWindow.document).attr("value", data.remark);
                            $("#selectedStatusName", layero.find("iframe")[0].contentWindow.document).attr("value", data.statusName);
                        }
                    });
                    break;
            }
        });

    });
</script>

</body>
</html>
